package com.example.redisdemo.config;

import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class Shiroconfig {

    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("defaultWebSecurityManager") DefaultWebSecurityManager defaultWebSecurityManager){
       //创建ShiroFilterFactoryBean 权限过滤对象
        ShiroFilterFactoryBean shiroFilterFactoryBean  = new ShiroFilterFactoryBean();
        //设置
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
        //创建一个集合来存权限
            Map<String, String>  map = new LinkedHashMap<>();

            //设置登入页面
            shiroFilterFactoryBean.setLoginUrl("/login");
             //设置未授权页面
        shiroFilterFactoryBean.setUnauthorizedUrl("/sq");
            //权限校验
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);

        return shiroFilterFactoryBean;
    }

    @Bean(name = "defaultWebSecurityManager")
    public  DefaultSecurityManager defaultSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
           DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
            defaultWebSecurityManager.setRealm(userRealm);
            return  defaultWebSecurityManager;

    }

    @Bean(name = "userRealm")
    public  UserRealm realm(){
        return  new UserRealm();

    }

}
